﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(TangentInspector))]
public class TangentInspectorEditor : E_ShowButtons<TangentInspector> { }
#endif
[ExecuteInEditMode]
public class TangentInspector : MonoBehaviour
{
    public Mesh mesh;
    public float length = 0.1f;
    Vector3[] ts;
    Vector3[] vs;
    Vector3[] ns;
    Vector3[] bs;
    public bool depthTest = true;
    [ShowButton]
    void Print()
    {
        mesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
        print(mesh.tangents.Length);
    }
    [ShowButton]
    void Start()
    {
        //mesh = new Mesh();
        //GetComponent<SkinnedMeshRenderer>().BakeMesh(mesh);
        //mesh.RecalculateTangents();
        mesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
        var tans = mesh.tangents;
        vs = mesh.vertices;
        ns = mesh.normals;
        ts = new Vector3[vs.Length];
        bs = new Vector3[vs.Length];
        for (int i = 0; i < vs.Length; i++)
        {
            vs[i] = transform.TransformPoint(vs[i]);
            ns[i] = transform.TransformDirection(ns[i]);
            ts[i] = transform.TransformDirection((Vector3)tans[i]);
            bs[i] = Vector3.Cross(ns[i], tans[i]) * tans[i].w;
        }
    }
    Mesh o;
    [ShowButton]
    void Apply()
    {
        if (o == null) o = GetComponent<SkinnedMeshRenderer>().sharedMesh;
        var m = Instantiate(o);
        m.RecalculateTangents();
        GetComponent<SkinnedMeshRenderer>().sharedMesh = m;
    }
    void Update()
    {
        if (vs.Empty()) return;
        int i = 0;
        foreach (var v in vs)
        {
            Debug.DrawRay(v, ns[i] * length, Color.blue, 0, depthTest);
            Debug.DrawRay(v, ts[i] * length, Color.red, 0, depthTest);
            Debug.DrawRay(v, bs[i++] * length, Color.green, 0, depthTest);
        }
    }
}
